package com.wayhome.srpingbootmybatis.mbs.tblyrule.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.wayhome.srpingbootmybatis.mbs.tblyrule.dao.TbdyRuleDao;
import com.wayhome.srpingbootmybatis.mbs.tblyrule.dto.DrgSetlAvgCostNewDTO;
import com.wayhome.srpingbootmybatis.mbs.util.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

@Slf4j
public class DrgSetlAvgCostNewListener extends AnalysisEventListener<DrgSetlAvgCostNewDTO> {

    private AtomicInteger successCnt = new AtomicInteger(0);
    private List<DrgSetlAvgCostNewDTO> list = new ArrayList<>(Constants.MAX_LIST_SIZE);

    public List<DrgSetlAvgCostNewDTO> getList() {
        return list;
    }

    private final TbdyRuleDao tbdyRuleDao;

    private List<String> genSaveSqlList = new ArrayList<>();
    private static String DOUHAO_SPLIT = "', ";
    private static String SET_SPLIT = "' ";
    private static String SET_END = "';";
    private static String INSERT_END = "');";

    public DrgSetlAvgCostNewListener(TbdyRuleDao tbdyRuleDao) {
        this.tbdyRuleDao = tbdyRuleDao;
    }

    @Override
    public void invoke(DrgSetlAvgCostNewDTO drgSetlAvgCostNewDTO, AnalysisContext analysisContext) {
        if (drgSetlAvgCostNewDTO == null) {
            return;
        }
        Date now = new Date();
        drgSetlAvgCostNewDTO.setValiFlag("1");
        drgSetlAvgCostNewDTO.setCrteTime(now);
        drgSetlAvgCostNewDTO.setUpdtTime(now);

        BigDecimal avgAmt = drgSetlAvgCostNewDTO.getAvgAmt();
        if (avgAmt == null) {
            drgSetlAvgCostNewDTO.setAvgAmt(BigDecimal.ZERO);
        } else {
            drgSetlAvgCostNewDTO.setAvgAmt(avgAmt.setScale(2, RoundingMode.HALF_UP));
        }
        BigDecimal medfeeSumamt = drgSetlAvgCostNewDTO.getMedfeeSumamt();
        if (medfeeSumamt == null) {
            drgSetlAvgCostNewDTO.setMedfeeSumamt(BigDecimal.ZERO);
        } else {
            drgSetlAvgCostNewDTO.setMedfeeSumamt(medfeeSumamt.setScale(2, RoundingMode.HALF_UP));
        }

        genSql(drgSetlAvgCostNewDTO);

//        list.add(drgSetlAvgCostNewDTO);
//        if (list.size() >= Constants.MAX_LIST_SIZE) {
//            int i = tbdyRuleDao.batchInsertDrgSetlAvgCostNew(list);
//            System.out.println("导入DRG次均费用（每解析五百条会触发一下），成功条数：" + successCnt.addAndGet(i));
//            list.clear();
//        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
//        log.info("DrgP次均费用导入调用完成");
//        if (!CollectionUtils.isEmpty(list)) {
//            int i = tbdyRuleDao.batchInsertDrgSetlAvgCostNew(list);
//            log.info("DrgP次均费用导入最后一次入库，条数为:{}, 最终导入成功条数:{}", list.size(),successCnt.addAndGet(i));
//            list.clear();
//        }
    }


    private void genSql(DrgSetlAvgCostNewDTO drgSetlAvgCostNewDTO) {
        DrgSetlAvgCostNewDTO dbData = tbdyRuleDao.queryDrgSetlAvgCostNew(drgSetlAvgCostNewDTO);

        StringBuilder stringBuilder  = new StringBuilder();
        if (!Objects.isNull(dbData)) {
            stringBuilder.append("update drg_setl_avg_cost_new_d set MEDFEE_SUMAMT = '")
                    .append(drgSetlAvgCostNewDTO.getMedfeeSumamt()).append(DOUHAO_SPLIT)
                    .append("SETL_CNT = '")
                    .append(drgSetlAvgCostNewDTO.getSetlCnt()).append(DOUHAO_SPLIT)
                    .append("AVG_AMT = '")
                    .append(drgSetlAvgCostNewDTO.getAvgAmt()).append(SET_SPLIT)
                    .append(" where id = '")
                    .append(dbData.getId()).append(SET_END);
        } else {
            stringBuilder.append("INSERT INTO drg_setl_avg_cost_new_d (MEDFEE_SUMAMT,SETL_CNT,AVG_AMT,SETL_LV,CLR_OPTINS," )
                    .append("DRG_CODG,DRG_NAME, SETL_YEAR) VALUE ('")
                    .append(drgSetlAvgCostNewDTO.getMedfeeSumamt()).append(DOUHAO_SPLIT)
                    .append(drgSetlAvgCostNewDTO.getSetlCnt()).append(DOUHAO_SPLIT)
                    .append(drgSetlAvgCostNewDTO.getAvgAmt()).append(DOUHAO_SPLIT)
                    .append(drgSetlAvgCostNewDTO.getSetlLv()).append(DOUHAO_SPLIT)
                    .append(drgSetlAvgCostNewDTO.getClrOptins()).append(DOUHAO_SPLIT)
                    .append(drgSetlAvgCostNewDTO.getDrgCodg()).append(DOUHAO_SPLIT)
                    .append(drgSetlAvgCostNewDTO.getDrgName()).append(DOUHAO_SPLIT)
                    .append(drgSetlAvgCostNewDTO.getSetlYear()).append(INSERT_END);

        }
        System.out.println(stringBuilder.toString());
        genSaveSqlList.add(stringBuilder.toString());

    }
}
